<template>
  <div>

    <el-button type="primary" @click="dialogFormVisible = true">添加咨询</el-button>
    <el-table :data="consultations" style="width: 100%">
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="content" label="内容" width="300"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="mini" @click="editConsultation(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteConsultation(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加咨询" v-model="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="内容" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="form.content" ></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="开放" value="OPEN"></el-option>
            <el-option label="关闭" value="CLOSED"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="addConsultation">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { QuillEditor } from '@vueup/vue-quill';
  import '@vueup/vue-quill/dist/vue-quill.snow.css';
  import {ElMessage} from "element-plus"; // 引入默认样式
export default {
  components: {
      QuillEditor,
    },
  data() {
    return {
      consultations: [],
      dialogFormVisible: false,
      form: {
        title: '',
        content: '',
        userId:1,
        status: ''
      },
      formLabelWidth: '140px'
    };
  },
  methods: {
    async getConsultations() {
      const response = await this.$axios.get('/consultation/list');
      this.consultations = response.data.data;
    },
    async addConsultation() {
      await this.$axios.post('/consultation/add', this.form);
      this.dialogFormVisible = false;
      this.getConsultations();
    },
    editConsultation(consultation) {
      // 实现编辑咨询的逻辑
    },
    async deleteConsultation(id) {
      await this.$axios.delete('/consultation/delete/'+id);
      ElMessage.success('删除成功');
      this.getConsultations();
    }
  },
  mounted() {
    this.getConsultations();
  }
};
</script>
